package com.system.service;

import java.util.List;
import java.util.ArrayList;

import com.system.dao.DaoFactory;
import com.system.dao.RemarkDao;
import com.system.dao.StudentDao;
import com.system.dao.TeacherDao;
import com.system.dao.UserDao;
import com.system.json.JsonContext;
import com.system.model.Student;
import com.system.model.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StudentService {
	private DaoFactory factory = DaoFactory.getInstance();
	private JsonContext context = new JsonContext();
	
//	//查找全部学生信息
//	public JSONObject queryAllStudent() {
//		StudentDao stuDao = factory.createStuDao();
//		ArrayList<Student> stus = stuDao.findAll();
//		
//	}
	//查询所有学生的信息
	public JSONObject queryAll() {
		StudentDao stuDao = factory.createStuDao();
		ArrayList<Student> students = stuDao.findAll();
		if(students==null){
			return context.getFailedObject("学生列表为空");
		}else{
			JSONArray array = new JSONArray();
			for(int i = 0; i < students.size(); i++) {
				Student stu = students.get(i);
				JSONObject json=JSONObject.fromObject(stu);
				array.add(json);
			}
			return context.getSuccessObject("查询成功", null, array);
		}
	}
	//根据user_id来查询学生信息
	public JSONObject queryInfo(int user_id) {
		StudentDao stuDao = factory.createStuDao();
		Student stu = stuDao.findById(user_id);
		if(stu==null){
			return context.getFailedObject("该学生不存在");
		}else{
			JSONObject json=JSONObject.fromObject(stu);
			return context.getSuccessObject("查询成功", json, null);
		}
	}
	//添加学生信息
	//【注】注意外键约束
	public JSONObject add(Student stu) {
		StudentDao stuDao = factory.createStuDao();
		UserDao userDao = factory.createUserDao();
		User user = new User();
		
		user.setUsername(stu.getStunum());
		user.setPassword("123456");
		user.setPost("student");
		boolean res = false;
		res = userDao.add(user);
		if (!res) {
			//在user表中重复
			return context.getFailedObject("该学生已注册");
		}
		
		try {
			user = userDao.findByUsername(stu.getStunum());
		} catch (Exception e) {
			e.printStackTrace();
		}
		stu.setUser_id(user.getId());
		res = stuDao.add(stu);
		if(!res){
			return context.getFailedObject("添加学生信息失败");
		}else{
			return context.getSuccessObject("添加学生信息成功", null, null);
		}
	}
	
	//修改学生信息
	public JSONObject modify(Student stu) {
		StudentDao stuDao = factory.createStuDao();
		boolean res = stuDao.modify(stu);
		if(!res){
			return context.getFailedObject("修改学生信息失败");
		}else{
			return context.getSuccessObject("修改学生信息成功", null, null);
		}
	}
	//删除学生信息
	public JSONObject deleteStudents(int[] stuIds) {
		StudentDao stuDao = factory.createStuDao();
		boolean res = stuDao.deleteStudents(stuIds);
		if(!res){
			return context.getFailedObject("删除学生信息失败");
		}else{
			return context.getSuccessObject("删除学生信息成功", null, null);
		}
	}
	/**
	 * 根据班级列出学生信息
	 * @param clazz
	 * @return
	 */
	public JSONObject checkoutQueryStusbyClass(String clazz){
		StudentDao stuDao=factory.createStuDao();
		List<Student> list=stuDao.findbyClass(clazz);
		if(list==null)
			return context.getFailedObject("查询结果为空");
		else{
			JSONArray arr=new JSONArray();
			for(Student student:list){
				JSONObject obj=new JSONObject();
				obj.put("stunum", student.getStunum());
				obj.put("stuname",student.getStuname());
				obj.put("stuclass", student.getStuclass());
				obj.put("major", student.getMajor());
				arr.add(obj);
			}
			return context.getSuccessObject("查询成功", null, arr);
		}
	}
	/**
	 * 给出自己的学号，查自己所在的班级，随后列出自己班级所在的学生
	 * @param stunum
	 * @return
	 */
	public JSONObject checkoutQueryStusbyStunum(String stunum){
		StudentDao stuDao=factory.createStuDao();
		String stuclass=stuDao.findClass(stunum);
		return this.checkoutQueryStusbyClass(stuclass);
	}
	
	public JSONObject checkoutAddRemarks(String stunum,String teanum,String remark){
		StudentDao stuDao=factory.createStuDao();
		TeacherDao teaDao=factory.createTeacherDao();
		RemarkDao remarkDao=factory.createRemarkDao();
		Integer stu_id=stuDao.findId(stunum);
		Integer tea_id=teaDao.findId(teanum);
		boolean res=remarkDao.addRemark(stu_id, tea_id, remark);
		if(!res)
			return context.getFailedObject("添加失败");
		else
			return context.getSuccessObject("添加成功", null, null);
	}
	/**
	 * 根据学号查询学生个人信息
	 * @param stunum
	 * @return
	 */
	public JSONObject checkoutqueryInfo(String stunum){
		StudentDao stuDao=factory.createStuDao();
		Student student=stuDao.querybystunum(stunum);
		if(student==null)
			return context.getFailedObject("查询失败，请输入正确的学号");
		else{
			JSONObject object=JSONObject.fromObject(student);
			return context.getSuccessObject("查询成功", object, null);
		}
	}
}
